package org.domeos.framework.api.controller.configuration;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.domeos.basemodel.HttpResponseTemp;
import org.domeos.basemodel.ResultStat;
import org.domeos.framework.api.controller.common.BaseSreController;
import org.domeos.framework.api.model.configuration.Configuration;
import org.domeos.framework.api.model.configuration.ConfigurationCollection;
import org.domeos.framework.api.service.configuration.ConfigurationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 
 * @author hanlongsen
 *
 */

@Controller
@RequestMapping("/sre/api/configurationcollection")
@Api("SRE部署相关API")
public class ConfigurationSreController extends BaseSreController {
	@Autowired
	ConfigurationService configurationService;
	@ApiOperation(value="创建配置集合")
	@ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
	public HttpResponseTemp<?> createConfigurationCollection(HttpServletRequest request ,@RequestBody ConfigurationCollection configurationCollection) {
		String userId = getUserId(request);
		if(StringUtils.isEmpty(userId)){
			return ResultStat.PARAM_ERROR.wrap("service userId is null!");
		}
		configurationCollection.setUserId(getIntValue(userId));
		return configurationService.sreCreateConfigurationCollection(configurationCollection);
    }
	

	@ApiOperation(value="配置集合信息查看")
	@ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
	public HttpResponseTemp<?> listConfigurationCollection(HttpServletRequest request) {	
		String userId = getUserId(request);
		if(StringUtils.isEmpty(userId)){
			return ResultStat.PARAM_ERROR.wrap("service userId is null!");
		}
        return configurationService.sreListConfigurationCollection();
    }
	
	@ApiOperation(value="创建配置信息")
	@ResponseBody
    @RequestMapping(value = "/{collectionId}/configuration/create", method = RequestMethod.POST)
	public HttpResponseTemp<?> sreCreateConfiguration(HttpServletRequest request,@PathVariable int collectionId, @RequestBody Configuration configuration) {
		String userId = getUserId(request);
		if(StringUtils.isEmpty(userId)){
			return ResultStat.PARAM_ERROR.wrap("service userId is null!");
		}
		configuration.setUserId(getIntValue(userId));
		return configurationService.sreCreateConfiguration(collectionId, configuration);
    }
	
	
	@ApiOperation(value="获取配置信息")
    @ResponseBody
    @RequestMapping(value = "/{collectionId}/configuration/list", method = RequestMethod.GET)
    public HttpResponseTemp<?> sreListConfigurationByCollectionId(HttpServletRequest request,@PathVariable int collectionId) {
		String userId = getUserId(request);
		if(StringUtils.isEmpty(userId)){
			return ResultStat.PARAM_ERROR.wrap("service userId is null!");
		}
		return configurationService.sreListConfigurationByCollectionId(collectionId);
    }
	
	
	@ApiOperation(value="获取配置信息")
    @ResponseBody
    @RequestMapping(value = "/configuration/list", method = RequestMethod.GET)
    public HttpResponseTemp<?> sreListAllConfigurations(HttpServletRequest request) {
		String userId = getUserId(request);
		if(StringUtils.isEmpty(userId)){
			return ResultStat.PARAM_ERROR.wrap("service userId is null!");
		}
        return configurationService.sreListAllConfigurations(getIntValue(userId));
    }
}
